//
//  ComplaintsViewController.m
//  Logistics
//
//  Created by hy002 on 2018/1/17.
//  Copyright © 2018年 HUYU. All rights reserved.
//

#import "ComplaintsViewController.h"
#import "ComplainTypeModel.h"

static NSString *cellPath = @"ComplaintsHomeCell";
@interface ComplaintsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSArray *allHallAry;
@property (nonatomic, assign)NSInteger closeType;

@end

@implementation ComplaintsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _closeType = 0;
    [self creatUI];
    [self requestAllHallList];
}

- (void)creatUI
{
    self.navigationItem.title = @"投诉";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = BacksColor;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-55*displaywidth-SCREER_HeiX) style:UITableViewStylePlain];
    _tableview.backgroundColor = BacksColor;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    UIButton *backBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, SCREEN_HEIGHT-STA_NAV_HEIGHT-55*displayheight-SCREER_HeiX, SCREEN_WIDTH-30*displaywidth, 45*displayheight) ImageName:nil Target:self Action:@selector(complaintsAction) Title:_titStr Font:16.0];
    backBtn.backgroundColor = YellowColor;
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    backBtn.layer.cornerRadius = 5;
    backBtn.layer.masksToBounds = YES;
    [self.view addSubview:backBtn];

}


//投诉
- (void)complaintsAction
{
    if (_closeType == 0)
    {
        [LTools showMBProgressWithText:@"请选择投诉理由" addToView:self.view];
        return;
    }
    [self requestComplainSave];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allHallAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = BacksColor;
    if (_allHallAry.count > 0)
    {
        ComplainTypeModel *model = _allHallAry[indexPath.row];
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 10*displaywidth, SCREEN_WIDTH, 45*displaywidth)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [cell.contentView addSubview:whiteView];
        UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 5*displaywidth, SCREEN_WIDTH- 60*displaywidth, 35*displaywidth) Font:15.0 Text:model.reason Color:RGB(51, 51, 51)];
        [whiteView addSubview:nameLab];
        NSString *imgStr = @"lb_icon27";
        if (indexPath.row +1 == _closeType)
        {
            imgStr = @"gs_icon12";
        }
        UIImageView *faceView = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-40*displaywidth, 10*displaywidth, 20*displaywidth, 25*displaywidth) ImageName:imgStr];
        faceView.contentMode = UIViewContentModeScaleAspectFit;
        [whiteView addSubview:faceView];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_closeType != (indexPath.row+1))
    {
        _closeType = indexPath.row+1;
        [tableView reloadData];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55*displaywidth;
}

//投诉发货人
- (void)requestComplainSave
{
    WEAKSELF;
    ComplainTypeModel *model = _allHallAry[_closeType-1];
    NSDictionary *dic = @{@"type":_typeStr,
                          @"complainType":model.reason,
                          @"joinId":_joinStr};
    [HYBaseHandler requestWithTheFormUrl:SERVER_complainSave Parmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            [LTools showMBProgressWithText:@"投诉成功" addToView:sharedDelegate.window];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}


//投诉类型
- (void)requestAllHallList
{
    WEAKSELF;
    [HYBaseHandler requestWithTheFormUrl:SERVER_complainType Parmers:nil Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            weakSelf.allHallAry = [ComplainTypeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
